/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contable.presentacion;

import contable.aplicacion.FactoriaSQL;
import contable.dominio.Cardex;
import contable.dominio.Inventario;
import contable.dominio.Periodo;
import java.util.LinkedList;

/**
 *
 * @author jaime
 */
public class InventarioCardex extends javax.swing.JFrame {

    private javax.swing.JFrame parent;
    private javax.swing.table.DefaultTableModel tabla;
    private Inventario producto;
    private LinkedList<Cardex> cardex;
    private FactoriaSQL fsql;

    /**
     * Creates new form InventarioCardex
     */
    public InventarioCardex(javax.swing.JFrame parent, Inventario producto) {
        this.producto = producto;
        initComponents();
        setLocationRelativeTo(parent);
        this.parent = parent;
        this.parent.setVisible(false);
        tabla = (javax.swing.table.DefaultTableModel) tabCardex.getModel();
        cardex = new LinkedList<>();
        fsql = FactoriaSQL.getINSTANCIA();
        cargarCardex();
    }

    @Override
    public void setTitle(String title) {
        switch (producto.getProducto()) {
            case 0:
                super.setTitle("Cardex de papel fotográfico");
                break;
            case 1:
                super.setTitle("Cardex de tintas para fotografía");
                break;
            case 2:
                super.setTitle("Cardex de empaques");
                break;
            case 3:
                super.setTitle("Cardex de fotografías");
                break;
            case 4:
                super.setTitle("Cardex de cámaras");
                break;
            case 5:
                super.setTitle("Cardex de memorias USB 2 Gb");
                break;
            case 6:
                super.setTitle("Cardex de memorias USB 4 Gb");
                break;
            case 7:
                super.setTitle("Cardex de memorias USB 8 Gb");
                break;
            case 8:
                super.setTitle("Cardex de memorias USB 16 Gb");
                break;
            case 9:
                super.setTitle("Cardex de estuches para cámara");
                break;
            case 10:
                super.setTitle("Cardex de marcos de fotografía");
                break;
            default:
                super.setTitle(title);
        }
    }

    @Override
    public void dispose() {
        parent.setVisible(true);
        super.dispose();
    }

    private void cargarCardex() {
        fsql.rollback(new Periodo(), cardex, producto);
        for (Cardex cdx : cardex) {
            if (cdx.esIngreso()) {
                tabla.addRow(new Object[]{cdx.getFecha(), cdx.getCantidad(), cdx.getCosto(), cdx.getMonto(), null, null, null});
            } else {
                tabla.addRow(new Object[]{cdx.getFecha(), null, null, null, cdx.getCantidad(), cdx.getCosto(), cdx.getMonto(), null, null, null});
            }
        }
        txtDisponible.setText(String.valueOf(producto.getCantidad()));
        txtCosto.setText(String.valueOf(producto.getCosto()));
        txtValor.setText(String.valueOf(producto.getMonto()));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabCardex = new org.jdesktop.swingx.JXTable();
        lblDisponible = new javax.swing.JLabel();
        txtDisponible = new javax.swing.JTextField();
        lblCosto = new javax.swing.JLabel();
        txtCosto = new javax.swing.JTextField();
        lblValor = new javax.swing.JLabel();
        txtValor = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cardex");

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabCardex.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Fecha", "Cantidad", "Costo unitario", "Monto", "Cantidad", "Costo unitario", "Monto"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabCardex.setName("tabCardex"); // NOI18N
        jScrollPane1.setViewportView(tabCardex);

        lblDisponible.setText("Cantidad disponible:");
        lblDisponible.setName("lblDisponible"); // NOI18N

        txtDisponible.setEditable(false);
        txtDisponible.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtDisponible.setName("txtDisponible"); // NOI18N

        lblCosto.setText("Costo unitario:");
        lblCosto.setName("lblCosto"); // NOI18N

        txtCosto.setEditable(false);
        txtCosto.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtCosto.setName("txtCosto"); // NOI18N

        lblValor.setText("Valor del inventario:");
        lblValor.setName("lblValor"); // NOI18N

        txtValor.setEditable(false);
        txtValor.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtValor.setName("txtValor"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblDisponible)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDisponible, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblCosto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCosto, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblValor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDisponible)
                    .addComponent(txtDisponible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblValor)
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCosto)
                    .addComponent(txtCosto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCosto;
    private javax.swing.JLabel lblDisponible;
    private javax.swing.JLabel lblValor;
    private org.jdesktop.swingx.JXTable tabCardex;
    private javax.swing.JTextField txtCosto;
    private javax.swing.JTextField txtDisponible;
    private javax.swing.JTextField txtValor;
    // End of variables declaration//GEN-END:variables
}
